-- UISkyBloodyTestbed
-- Created by huanghr July/13/2016
-- 血腥试验台

UISkyBloodyTestbed = class("UISkyBloodyTestbed", function()
    return cc.Layer:create();
end);

function UISkyBloodyTestbed.create(gridData)
    return UISkyBloodyTestbed.new(gridData);
end

-- 按钮数量
local BTN_COUNT = 6;

local materialList = {7431, 7432, 7433, 7434, 7435, 7436};

-- 制作动画播放时间
local PRODUCE_TIME = 1.2;

-- 制作完成后，界面停留的时间
local STAY_TIME = 0.6;

-- 构造函数
function UISkyBloodyTestbed:ctor(gridData)
    self:setName("UISkyBloodyTestbed");

    -- 复用保险箱建筑界面
    local node = cc.CSLoader:createNode("layout/dungeon/special/BloodyTestbed.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;

    -- 点击过的按钮数据
    self.clickData = {};

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UISkyBloodyTestbed:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UISkyBloodyTestbed:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UISkyBloodyTestbed 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UISkyBloodyTestbed:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UISkyBloodyTestbed");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));


    -- 注册点击事件
    self.buttons = {};
    local btnNode = findChildByName(node, "CT/btn_node");
    for i = 1, BTN_COUNT do
        local btn = findChildByName(btnNode, "btn_"..i);
        btn.classId = materialList[i];
        table.insert(self.buttons, btn);
        local function onOkClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");

                if ItemM.getAmount(ME.user, sender.classId) < 1 then
                    alert(getLocStr("forge_spade_material_lock"));
                    return false;
                end

                table.insert(self.clickData, sender.classId);

                sender:setTouchEnabled(false);
                sender:setBright(false);

            end
        end
        AddTouchEventListener(btn, onOkClick);
        TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        local name = ItemM.query(btn.classId, "name");

        btn:setTitleText(name);
    end

    -- 调制按钮
    local btnTest = findChildByName(btnNode, "btn_test");
    local function onTestClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            self:tryTest();
        end
    end

    AddTouchEventListener(btnTest, onTestClick);
    TextStyleM.setTextStyle(btnTest, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);
    btnTest:setTitleText(buildingInfo["view_text"]);
end

-- 重绘
function UISkyBloodyTestbed:redraw()
    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    titleLabel:setString(buildingInfo["title"]);
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    applyString(descLabel, buildingInfo["dialog"]);
end


-- 尝试开启
function UISkyBloodyTestbed:tryTest()
    if #self.clickData < 1 then
        alert(getLocStr("need_item_first"));
        return;
    end

    -- 按钮短暂置灰
    wait(PRODUCE_TIME + 0.7);

    -- 播放制作动画
    self:playProduceEffect();

    local function doCompound()
        local items = FormulaM.invoke("CALC_MATERIAL_TO_NUMBER", self.clickData);
        DungeonActionM.go("testbed_concoct", self.gridData:getPos(), items);

        UIMgr.getCurrentScene():removeFormByName("UISkyBloodyTestbed");
    end
    performWithDelay(self, doCompound, PRODUCE_TIME);
end

-- 播放制作动画
function UISkyBloodyTestbed:playProduceEffect()
    -- 播放制作特效
    local effectNode = findChildByName(self.node, "CT/icon");
    playEffect(effectNode, 1151, 150, 0, nil, 0.7);
end